/*
 * @Descripttion: 核酸宝项目前端
 * @version: 
 * @Author: 杜宇轩
 * @Date: 2022-05-11 21:42:49
 * @LastEditors: 杜宇轩
 * @LastEditTime: 2022-05-22 06:18:34
 */
import { getRegionInfo } from '@/api/Info/getRegionInfo';

const region = {
  namespaced: true,
  state: {
  },

  getters: {
  },

  mutations: {
  },

  actions: {
    getAllInfo({}, option) {
      return new Promise((resolve, reject) => {
        const TreeRegion = {};
        const res = [];
        getRegionInfo().then((data) => {
          data.forEach(item => {
            if (!TreeRegion[`${item.province}`]) {
              TreeRegion[`${item.province}`] = {};
            }
            if (!TreeRegion[`${item.province}`][`${item.city}`]) {
              TreeRegion[`${item.province}`][`${item.city}`] = {};
            }
            if (!TreeRegion[`${item.province}`][`${item.city}`][`${item.area}`]) {
              TreeRegion[`${item.province}`][`${item.city}`][`${item.area}`] = {};
            }
            TreeRegion[`${item.province}`][`${item.city}`][`${item.area}`][`${item.testLocation}`] = item.regionId;
          });

          for (const province in TreeRegion) {
            const obj_p = {};
            obj_p['label'] = province;
            const province_child = [];
            for (const city in TreeRegion[province]) {
              const obj_c = {};
              obj_c['label'] = city;
              const city_child = [];
              for (const area in TreeRegion[province][city]) {
                const obj_a = {};
                obj_a['label'] = area;
                const area_child = [];
                for (const testLocation in TreeRegion[province][city][area]) {
                  const obj_t = {};
                  obj_t['label'] = testLocation;
                  obj_t['value'] = TreeRegion[province][city][area][testLocation];
                  area_child.push(obj_t);
                }
                obj_a['children'] = area_child;
                city_child.push(obj_a);
              }
              obj_c['children'] = city_child;
              province_child.push(obj_c);
            }
            obj_p['children'] = province_child;
            res.push(obj_p);
          }
          if (option === 0) {
            resolve(data);
          } else if (option === 1) {
            resolve(res);
          }
        })
      });
    }
  }

};

export default region;
  